package com.cskt.controller;

import com.cskt.pojo.Role;
import com.cskt.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by 曾小丽 on 2018/4/9.
 */
@Controller
public class RoleController {
    @Autowired
    private RoleService roleService;


    @RequestMapping("show")
    public  String getAllInfo(Model model){
        System.out.println("测试111111");
        List<Role> getAllInfo=roleService.getAllInfo();
        model.addAttribute("roleInfo",getAllInfo);
        return "index";
    }


    @RequestMapping("del")
    public void delUser(int rid, HttpServletRequest request,HttpServletResponse response){
        String result = "{\"result\":\"error\"}";
        if(roleService.Del(rid)){
            result = "{\"result\":\"success\"}";
        }
        response.setContentType("application/json");
        try {
            PrintWriter out = response.getWriter();
            out.write(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("add")
    public  String add(@ModelAttribute ("role") Role role ){
        return "add";
    }


    @RequestMapping("update")
    public  String update(@ModelAttribute ("role") Role role,HttpServletRequest request,Model model){
        String rid=request.getParameter("rid");
        model.addAttribute("rid",rid);
        return "update";
    }




    @RequestMapping("addInfo")
    public  String addInfo(Role role){
       /* Role role=new Role();
        role.setRoleCode(roleCode);
        role.setRoleName(roleName);
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date time=(Date) sdf.parse(creationDate);
            role.setCreationDate(time);
           int info= roleService.Add(role);
            if(info>0){
                System.out.println("┗|｀O′|┛ 嗷~~新增succes");
            }else{
                System.out.println("┗|｀O′|┛ 嗷~~新增失败");
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }*/
        int info= roleService.Add(role);
        if(info>0){
            System.out.println("┗|｀O′|┛ 嗷~~新增succes");
        }else{
            System.out.println("┗|｀O′|┛ 嗷~~新增失败");
        }
        return "redirect:show";
    }



    @RequestMapping("updateInfo")
    public  String updateInfo(HttpServletRequest request ){
        Role role=new Role();
        String id=request.getParameter("id");
        String roleCode=request.getParameter("roleCode");
        String roleName=request.getParameter("roleName");
        String createDate=request.getParameter("creationDate");
        role.setId(Integer.parseInt(id));
        role.setRoleCode(roleCode);
        role.setRoleName(roleName);
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date time=(Date) sdf.parse(createDate);
            role.setCreationDate(time);
            int info= roleService.Update(role);
            if(info>0){
                System.out.println("┗|｀O′|┛ 嗷~~修改succes");
            }else{
                System.out.println("┗|｀O′|┛ 嗷~~修改失败");
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "redirect:show";
    }
}
